<%@page import="java.util.UUID"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp"%>
<%
	String uuid = UUID.randomUUID().toString();
	request.setAttribute("uuid", uuid);
%>
<div class="container-fluid am-margin-top">
	<form id="${uuid}_form">
		<input type="hidden" name="id">
		<input type="hidden" name="funcPointId">
		<input type="hidden" name="dataModelGroupCode">
		<div class="row">
			<div class="col-sm-6">
				<div class="form-group ">
					<label>名称</label>
					<input type="text" name="title" required class="form-control">
				</div>
			</div>
			<div class="col-sm-6">
				<div class="form-group">
					<label>编号</label>
					<input type="text" name="code" required class="form-control">
				</div>
			</div>
		</div>
		<div class="form-group">
			<label>视图类型：</label>
			<select name="viewType">
				<pf:dataDic var="item" dicCode="QUERY_DEF_VIEWTYPE">
	                 <option value="${item.value}">${item.name}</option>
	            </pf:dataDic>
			</select>
		</div>
		<div class="form-group">
			<label>SQL或表名</label>
			<textarea name="sqlStr" required class="form-control"></textarea>
		</div>
		<div class="form-group ">
			<label>加载脚本路径</label>
			<input type="text" name="scriptPath" class="form-control">
		</div>
		<div class="form-group ">
			<a href="javascript:void(0);" id="${uuid}_submitBt" class="btn btn-primary pull-right">提交</a>
		</div>
	</form>
</div>
<script type="text/javascript">
	$(function() {
		var id = '${param.id}';
		var dataModelGroupCode='${param.dataModelGroupCode}';
		var uuid = '${uuid}';
		var form = $("#" + uuid + "_form");
		var formValidation = form.validation();

		$("#" + uuid + "_submitBt").click(
				function() {
					if (isValid()) {
						var formObj=webUtils.formToObj(form);
						$.post(ctx+"/queryDefAction/saveQueryDef.do",{datas:Base64.encode(JSON.stringify(formObj))},function(data){
							if (data.success) {
								webUtils.alert("提交成功");
								top.webUtils.topicPublish(
										"queryDefine.edit.success", data);
		                    } else {
		                    	webUtils.error(data.msg);
		                    }
						},"json");
					}
				});

		function isValid() {
			return formValidation.valid();
		}

		var init = function() {
			if(dataModelGroupCode){
				form.find("input[name=dataModelGroupCode]").val(dataModelGroupCode);
			}
		}

		if (id) {
			$.getJSON(ctx + '/queryDefAction/findQueryDefById.do', {
				id : id
			}, function(data) {
				webUtils.fullForm({
					form : form,
					data : data
				});

				init();

			});
		} else {
			init();
		}

	});
</script>